﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Acr.XamForms.UserDialogs;
using HalalCheck.CustomViews;
using HalalCheck.DataModels;
using HalalCheck.Resources;
using HalalCheck.ViewModels;
using SpotOffer.Resource;
using SpotOffer.Utils;
using Xamarin;
using Xamarin.Forms;
using XLabs.Forms.Mvvm;
using XLabs.Ioc;
using XLabs.Platform.Services;

namespace HalalCheck.View
{
    public class Root : NavigationPage
    {
        MenuPage _menu;

        public Root()
        {
            BarBackgroundColor = AppColors.OrigDarkBlue;
            BarTextColor = AppColors.White;

            _menu = (ViewFactory.CreatePage<MenuViewModel, Page>() as MenuPage);
            _menu.MenuListView.ItemSelected += (sender, e) =>
            {
                if (e.SelectedItem != null)
                {
                    NavigateTo(e.SelectedItem as MenuItemHalalCheck);
                    ((ListView)sender).SelectedItem = null; // de-select the row
                }
            };
            PushAsync(_menu);

            // _menu.MenuListView.SelectedItem = _menu.MenuListView.ItemsSource.Cast<MenuItemHalalCheck>().First();
        }

        private async void NavigateTo(MenuItemHalalCheck menu)
        {

            var reachable = Resolver.Resolve<INetwork>();
            if (((menu.ViewModelType == typeof(SalahViewModel) || 
                menu.ViewModelType == typeof(QuranViewModel)) ||
                menu.ViewModelType == typeof(LiveMakkahViewModel)||
                menu.ViewModelType == typeof(LiveMadinaViewModel))
                &reachable.InternetConnectionStatus().ToString().Contains("NotReachable"))
            {
                Insights.Track("MenuClicked", new Dictionary<string, string> { { "MenuClicked", menu.ViewModelType.Name +" " +reachable.InternetConnectionStatus()}, });
                DependencyService.Get<IUserDialogService>().Alert(HalalStrings.CheckInternet+": " + menu.Title, HalalStrings.Error);
            }
            else
            {
                Insights.Track("MenuClicked", new Dictionary<string, string> { { "MenuClicked", menu.ViewModelType.Name }, });
                Page displayPage = (Page)ViewFactory.CreatePage(menu.ViewModelType);
                await PushAsync(displayPage);
            }
        }

    }
}